package com.gewu.teday.utils
import android.os.Parcelable

import com.tencent.mmkv.MMKV

class SpUtils {


    init {
        mv = MMKV.defaultMMKV()
    }

    companion object {
        private var mInstance: SpUtils? = null
        private var mv: MMKV? = null

        /*** 初始化MMKV,只需要初始化一次，建议在Application中初始化  */
        val instances: SpUtils?
            get() {
                if (mInstance == null) {
                    synchronized(SpUtils::class.java) {
                        if (mInstance == null) {
                            mInstance = SpUtils()
                        }
                    }
                }
                return mInstance
            }

    }

        /*** 保存数据的方法，我们需要拿到保存数据的具体类型，然后根据类型调用不同的保存方法  */
        fun encode(key: String?, anys: Any) {
            when (anys) {
                is String -> {
                    mv!!.encode(key, anys)
                }
                is Int -> {
                    mv!!.encode(key, anys)
                }
                is Boolean -> {
                    mv!!.encode(key, anys)
                }
                is Float -> {
                    mv!!.encode(key, anys)
                }
                is Long -> {
                    mv!!.encode(key, anys)
                }
                is Double -> {
                    mv!!.encode(key, anys)
                }
                is ByteArray -> {
                    mv!!.encode(key, anys)
                }
                else -> {
                    mv!!.encode(key, anys.toString())
                }
            }
        }

        fun encodeSet(key: String?, sets: Set<String>) {
            mv!!.encode(key, sets)
        }

        fun encodeParcelable(key: String?, obj: Parcelable?) {
            mv!!.encode(key, obj)
        }

        /**得到保存数据的方法，我们根据默认值得到保存的数据的具体类型，然后调用相对于的方法获取值 */
        fun decodeInt(key: String?): Int {
            return mv!!.decodeInt(key, 0)
        }

        fun decodeDouble(key: String?): Double {
            return mv!!.decodeDouble(key, 0.00)
        }

        fun decodeLong(key: String?): Long {
            return mv!!.decodeLong(key, 0L)
        }

        fun decodeBoolean(key: String?): Boolean {
            return mv!!.decodeBool(key, false)
        }

        fun decodeFloat(key: String?): Float {
            return mv!!.decodeFloat(key, 0f)
        }

        fun decodeBytes(key: String?): ByteArray {
            return mv!!.decodeBytes(key)
        }

        fun decodeString(key: String?): String {
            return mv!!.decodeString(key, "0")
        }

        fun <T : Parcelable>decodeParcelable(key:String, tClass:Class<T> ): T? {
            if (mv==null){
                return null
            }
            return mv!!.decodeParcelable(key,tClass)
        }

        /*** 移除某个key对 ** @param key  */
        fun removeKey(key: String?) {
            mv!!.removeValueForKey(key)
        }

        /*** 清除所有key  */
        fun clearAll() {
            mv!!.clearAll()
        }


}